"""Copyright 2011 Thomas Anesta, Jonathan Brenner, Tyler Moylat, Rosa Tung, Sean Kim"""

"""

This file is part of Bubbles

Bubbles is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Bubbles is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

"""

import sys, pygame
import BubblesConstants
import math
import random

powerup_width = 15
powerup_height = 15
powerup_x = 25
powerup_y = 25
powerup_vel = 4
powerup_offset = 1#the value of which to change the bubble

POWERUP_MAX_OFFSET = 1
POWERUP_MIN_OFFSET = 1

def get_rand_offset():
    return random.randint(POWERUP_MIN_OFFSET, POWERUP_MIN_OFFSET)

class PowerUp(object):

    def __init__(self, x, y, offset):#change to x, and y
        self.type = BubblesConstants.POWER_UP_TYPE
        self.status = BubblesConstants.STATUS_ACTIVE
        if offset is None:
            self.image = pygame.image.load("gfx/Power_L.png").convert_alpha()
            self.rect = self.image.get_rect()
            self.rect.width = powerup_width
            self.rect.height = powerup_height
            self.offset = powerup_offset
        elif offset >= 0:#it is a plus
            self.image = pygame.image.load("gfx/Power_L.png").convert_alpha()
            self.rect = self.image.get_rect()
            self.rect.width = powerup_width
            self.rect.height = powerup_height
            self.offset = offset
        else:
            self.image = pygame.image.load("gfx/Power_S.png").convert_alpha()
            self.rect = self.image.get_rect()
            self.rect.width = powerup_width
            self.rect.height = powerup_height
            self.offset = offset
        #end value if
        if x is not None:
            self.rect.x = x
        else:
            self.rect.x = powerup_x
        if y is not None:
            self.rect.y = y
        else:
            self.rect.y = powerup_y
        self.vel = powerup_vel
        

        
    def update(self, player):#how are we going to handle collisions with enemies?
        if(self.status == BubblesConstants.STATUS_DEAD):
            pass#if it's dead just wait to destroy
        future = self.rect.move(0, self.vel)
        self.rect = future
        #self.y should never be less than 0 because this spawns on screen
        rsum = self.rect.width/2 + player.rect.width/2#sum of the radii
        cdistance = math.sqrt( ( (self.rect.center[0] - player.rect.center[0])**2 ) + ( (self.rect.center[1] - player.rect.center[1])**2 ) )
        if cdistance < rsum:
            self.kill()
            return BubblesConstants.COLLISION_PLAYER_RETURN#signifies collision with player
        elif future.top > BubblesConstants.WINDOW_HEIGHT - BubblesConstants.WINDOW_HUD:
            self.leave()
            return BubblesConstants.OFF_SCREEN_RETURN#signifies offscreen
        else:
            return BubblesConstants.NO_COLLISION_RETURN#signifies no collisions
    
    def draw(self, screen):
        """draws the power up"""
        screen.blit(self.image, self.rect)
        
    def leave(self):
        self.status = BubblesConstants.STATUS_OFFSCREEN
    
    def kill(self):
        self.status = BubblesConstants.STATUS_DEAD
    